// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   invoice_garbage_ok.java

package draft.crm;

import include.nseer_cookie.counter;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.StringTokenizer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;

public class invoice_garbage_ok extends HttpServlet
{

	ServletContext application;
	HttpSession session;
	nseer_db_backup1 erp_db;

	public invoice_garbage_ok()
	{
		erp_db = null;
	}

	public synchronized void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		ServletContext dbApplication = dbSession.getServletContext();
		try
		{
			ValidataNumber validata = new ValidataNumber();
			nseer_db_backup1 crm_db = new nseer_db_backup1(dbApplication);
			if (crm_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				counter count = new counter(dbApplication);
				java.io.PrintWriter out = response.getWriter();
				String product_amount = request.getParameter("product_amount");
				String reasonexact = request.getParameter("reasonexact");
				String which_time = request.getParameter("which_time");
				int num = Integer.parseInt(product_amount);
				String reason = request.getParameter("reason");
				String customer_ID = request.getParameter("customer_ID");
				String customer_name = request.getParameter("customer_name");
				String real_customer_mailing_address = request.getParameter("real_customer_mailing_address");
				String real_contact_person = request.getParameter("real_contact_person");
				String real_contact_person_tel = request.getParameter("real_contact_person_tel");
				String real_contact_person_fax = request.getParameter("real_contact_person_fax");
				String real_invoice_time = request.getParameter("real_invoice_time");
				String real_invoice_type = request.getParameter("real_invoice_type");
				String register = request.getParameter("register");
				String register_ID = request.getParameter("register_ID");
				String invoice_group = request.getParameter("invoice_group");
				String register_time = request.getParameter("register_time");
				double invoiced_subtotal_sum = 0.0D;
				double invoiced_subtotal_sum_all = 0.0D;
				int p = 0;
				for (int i = 1; i <= num; i++)
				{
					String tem_invoice_sum = (new StringBuilder("invoice_sum")).append(i).toString();
					String invoice_sum2 = request.getParameter(tem_invoice_sum);
					if (invoice_sum2.equals(""))
						invoice_sum2 = "0";
					StringTokenizer tokenTO2 = new StringTokenizer(invoice_sum2, ",");
					String invoice_sum;
					String invoice_sum1;
					for (invoice_sum = ""; tokenTO2.hasMoreTokens(); invoice_sum = (new StringBuilder(String.valueOf(invoice_sum))).append(invoice_sum1).toString())
						invoice_sum1 = tokenTO2.nextToken();

					if (!validata.validata(invoice_sum))
						p++;
				}

				if (p == 0)
				{
					int n = 0;
					if (n == 0)
					{
						String sql8 = (new StringBuilder("select * from crm_order where order_id='")).append(reasonexact).append("' and (invoice_check_tag='9' or invoice_check_tag='5') and which_time='").append(which_time).append("'").toString();
						ResultSet rs8 = crm_db.executeQuery(sql8);
						if (rs8.next())
						{
							for (int j = 1; j <= num; j++)
							{
								String tem_details_number = (new StringBuilder("details_number")).append(j).toString();
								String tem_id = (new StringBuilder("id")).append(j).toString();
								String id = request.getParameter(tem_id);
								String s = request.getParameter(tem_details_number);
							}

							String product_ID_control1 = request.getParameter("product_ID1");
							String subtotal_control1 = request.getParameter("invoiced_subtotal1");
							double subtotal_control = Double.parseDouble(subtotal_control1);
							for (int j = 1; j <= num; j++)
							{
								invoiced_subtotal_sum = 0.0D;
								String tem_details_number = (new StringBuilder("details_number")).append(j).toString();
								String tem_product_name = (new StringBuilder("product_name")).append(j).toString();
								String tem_product_ID = (new StringBuilder("product_ID")).append(j).toString();
								String tem_subtotal = (new StringBuilder("subtotal")).append(j).toString();
								String tem_invoice_sum = (new StringBuilder("invoice_sum")).append(j).toString();
								String tem_invoiced_subtotal = (new StringBuilder("invoiced_subtotal")).append(j).toString();
								String tem_remark = (new StringBuilder("remark")).append(j).toString();
								String tem_id = (new StringBuilder("id")).append(j).toString();
								String id = request.getParameter(tem_id);
								String product_name = request.getParameter(tem_product_name);
								String product_ID = request.getParameter(tem_product_ID);
								String details_number = request.getParameter(tem_details_number);
								String subtotal = request.getParameter(tem_subtotal);
								String invoiced_subtotal = request.getParameter(tem_invoiced_subtotal);
								String invoice_sum2 = request.getParameter(tem_invoice_sum);
								if (invoice_sum2.equals(""))
									invoice_sum2 = "0";
								StringTokenizer tokenTO1 = new StringTokenizer(invoice_sum2, ",");
								String invoice_sum;
								String invoice_sum1;
								for (invoice_sum = ""; tokenTO1.hasMoreTokens(); invoice_sum = (new StringBuilder(String.valueOf(invoice_sum))).append(invoice_sum1).toString())
									invoice_sum1 = tokenTO1.nextToken();

								String remark = request.getParameter(tem_remark);
								if (product_ID.equals(product_ID_control1))
								{
									if (invoice_sum != null && Double.parseDouble(invoice_sum) != 0.0D)
									{
										subtotal_control += Double.parseDouble(invoice_sum);
										invoiced_subtotal_sum_all += Double.parseDouble(invoice_sum);
										String sql1 = (new StringBuilder("update crm_ordering set reason='")).append(reason).append("',customer_ID='").append(customer_ID).append("',customer_name='").append(customer_name).append("',real_customer_mailing_address='").append(real_customer_mailing_address).append("',real_contact_person='").append(real_contact_person).append("',real_contact_person_tel='").append(real_contact_person_tel).append("',real_contact_person_fax='").append(real_contact_person_fax).append("',real_invoice_time='").append(real_invoice_time).append("',real_invoice_type='").append(real_invoice_type).append("',register='").append(register).append("',register_ID='").append(register_ID).append("',register_time='").append(register_time).append("',product_ID='").append(product_ID).append("',product_name='").append(product_name).append("',invoice_sum='").append(invoice_sum).append("',remark='").append(remark).append("',invoice_group='").append(invoice_group).append("' where id='").append(id).append("'").toString();
										crm_db.executeUpdate(sql1);
										if (subtotal_control == Double.parseDouble(subtotal))
										{
											String sql2 = (new StringBuilder("update crm_order_details set invoiced_subtotal='")).append(subtotal_control).append("',invoice_tag='1' where order_ID='").append(reasonexact).append("' and details_number='").append(details_number).append("'").toString();
											crm_db.executeUpdate(sql2);
										} else
										{
											String sql3 = (new StringBuilder("update crm_order_details set invoiced_subtotal='")).append(subtotal_control).append("' where order_ID='").append(reasonexact).append("' and details_number='").append(details_number).append("'").toString();
											crm_db.executeUpdate(sql3);
										}
									} else
									if (Double.parseDouble(invoice_sum) == 0.0D)
									{
										String sql7 = (new StringBuilder("delete from crm_ordering where id='")).append(id).append("'").toString();
										crm_db.executeUpdate(sql7);
									}
								} else
								if (invoice_sum != null && Double.parseDouble(invoice_sum) != 0.0D)
								{
									product_ID_control1 = product_ID;
									subtotal_control = Double.parseDouble(invoiced_subtotal) + Double.parseDouble(invoice_sum);
									invoiced_subtotal_sum_all += Double.parseDouble(invoice_sum);
									String sql1 = (new StringBuilder("update crm_ordering set reason='")).append(reason).append("',customer_ID='").append(customer_ID).append("',customer_name='").append(customer_name).append("',real_customer_mailing_address='").append(real_customer_mailing_address).append("',real_contact_person='").append(real_contact_person).append("',real_contact_person_tel='").append(real_contact_person_tel).append("',real_contact_person_fax='").append(real_contact_person_fax).append("',real_invoice_time='").append(real_invoice_time).append("',real_invoice_type='").append(real_invoice_type).append("',register='").append(register).append("',register_ID='").append(register_ID).append("',register_time='").append(register_time).append("',product_ID='").append(product_ID).append("',product_name='").append(product_name).append("',invoice_sum='").append(invoice_sum).append("',remark='").append(remark).append("',invoice_group='").append(invoice_group).append("' where id='").append(id).append("'").toString();
									crm_db.executeUpdate(sql1);
									if (subtotal_control == Double.parseDouble(subtotal))
									{
										String sql2 = (new StringBuilder("update crm_order_details set invoiced_subtotal='")).append(subtotal_control).append("',invoice_tag='1' where order_ID='").append(reasonexact).append("' and details_number='").append(details_number).append("'").toString();
										crm_db.executeUpdate(sql2);
									} else
									{
										String sql3 = (new StringBuilder("update crm_order_details set invoiced_subtotal='")).append(subtotal_control).append("' where order_ID='").append(reasonexact).append("' and details_number='").append(details_number).append("'").toString();
										crm_db.executeUpdate(sql3);
									}
								} else
								if (Double.parseDouble(invoice_sum) == 0.0D)
								{
									String sql7 = (new StringBuilder("delete from crm_ordering where id='")).append(id).append("'").toString();
									crm_db.executeUpdate(sql7);
								}
							}

							String sql2 = (new StringBuilder("update crm_order set invoice_check_tag='2' where order_ID='")).append(reasonexact).append("'").toString();
							crm_db.executeUpdate(sql2);
							response.sendRedirect("draft/crm/invoice_ok.jsp?finished_tag=2");
						} else
						{
							response.sendRedirect("draft/crm/invoice_ok.jsp?finished_tag=1");
						}
					} else
					{
						response.sendRedirect((new StringBuilder("draft/crm/invoice_ok_a.jsp?order_ID=")).append(reasonexact).toString());
					}
				} else
				{
					response.sendRedirect((new StringBuilder("draft/crm/invoice_ok_b.jsp?order_ID=")).append(reasonexact).toString());
				}
				crm_db.commit();
				crm_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception exception) { }
	}
}
